package com.aube;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;

import com.android.volley.Response;
import com.aube.activity.WapActivity;
import com.aube.push.PushSetting;
import com.aube.video.PlayActivity;
import com.huyn.baseframework.dynamicload.CanCloseLayout;
import com.huyn.baseframework.dynamicload.DLBridge;
import com.huyn.baseframework.dynamicload.OnShareChannelSelected;
import com.huyn.baseframework.model.Category;
import com.huyn.baseframework.model.ChannelDetail;
import com.huyn.baseframework.net.BFJSONRequest;
import com.huyn.baseframework.net.HttpRequestLoader;
import com.huyn.baseframework.share.ShareModule;
import com.huyn.baseframework.share.ShareUtil;
import com.huyn.baseframework.share.WeiboUtil;
import com.huyn.baseframework.share.WxUtil;
import com.huyn.baseframework.utils.AppUtil;
import com.huyn.baseframework.utils.StringUtils;
import com.huyn.baseframework.utils.UserUtil;
import com.ryg.dynamicload.DLProxyFragmentActivity;
import com.ryg.dynamicload.internal.DLIntent;
import com.ryg.dynamicload.internal.DLPluginManager;

import java.util.HashMap;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

/**
 * Created by huyaonan on 16/6/12.
 */
public class BaseActivity extends SwipeBackActivity implements DLBridge {

    private SwipeBackLayout mSwipeBackLayout;

    public View mShareLayout, mShareWechat, mShareWeibo, mShareTimeline;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppUtil.setTranslucentStatus(this, true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initSwipe();
    }

    private void initSwipe() {
        mSwipeBackLayout = getSwipeBackLayout();
        mSwipeBackLayout.addSwipeListener(new SwipeBackLayout.SwipeListener() {
            @Override
            public void onScrollStateChange(int state, float scrollPercent) {
            }

            @Override
            public void onEdgeTouch(int edgeFlag) {
            }

            @Override
            public void onScrollOverThreshold() {
            }
        });
        if(enableSwipeAction()) {
            mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
            mSwipeBackLayout.setEnableGesture(true);
        } else
            mSwipeBackLayout.setEnableGesture(false);
    }

    protected boolean enableSwipeAction() {
        return true;
    }

    @Override
    public void onClickToVideo(ChannelDetail channelDetail) {
        Intent intent = new Intent(this, PlayActivity.class);
        intent.putExtra(PlayActivity.CHANNEL_ITEM, channelDetail);
        startActivity(intent);
//        startActivity(new Intent(this, TinkerFixActivity.class));
    }

    @Override
    public void onClickToVideo(ChannelDetail channelDetail, Category category) {
        Intent intent = new Intent(this, PlayActivity.class);
        intent.putExtra(PlayActivity.CHANNEL_ITEM, channelDetail);
        intent.putExtra(PlayActivity.CATEGORY_ITEM, category);
        startActivity(intent);
//        Intent intent = new Intent(Intent.ACTION_VIEW);
//        intent.setData(Uri.parse("aube://com.aube.tv"));
//        startActivity(intent);
    }
    
    @Override
    public void onClickToWap(String title, String url, boolean post) {
        Intent intent = new Intent(this, WapActivity.class);
        intent.putExtra(WapActivity.WEB_TITLE, title);
        intent.putExtra(WapActivity.WEB_URL, url);
        intent.putExtra(WapActivity.WEB_POST, post);
        startActivity(intent);
    }

    @Override
    public void openPluginActivity(DLIntent intent) {
        DLPluginManager.getInstance(this).startPluginActivity(this, intent, DLProxyFragmentActivity.class);
    }

    @Override
    public boolean isUserLogin() {
        if(UserUtil.isUserLogin())
            return true;
        showLoginLayout();
        return false;
    }

    @Override
    public void doUserLogin() {
        WxUtil.wxLoginRequest(this);
    }

    @Override
    public void loginByChannel(int channel) {
        doLogin(channel);
    }

    @Override
    public void doShare(ShareModule shareModule) {
        //设置缩略图。最终压缩过的缩略图大小不得超过32kb。
        if(shareModule.imgBm == null)
            shareModule.imgBm = BitmapFactory.decodeResource(StaticApplicationContext.context.getResources(), R.drawable.icon_rect);
        ShareUtil.startShare(this, shareModule);
    }

    @Override
    public void shareWeiboVideo(String url, String content, Bitmap bitmap) {
        if(bitmap == null) {
            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_rect);
        }
        WeiboUtil.getInstance(this).startShareVideo(this, new ShareModule(content, url, bitmap));
    }

    @Override
    public void shareWeiboImg(String path, String content) {
        ShareModule shareModule = new ShareModule();
        shareModule.imagePath = path;
        shareModule.content = content;
        WeiboUtil.getInstance(this).startShareImg(this, shareModule);
    }

    @Override
    public boolean isWxInstalled() {
        return ShareUtil.isWXAppInstalled();
    }

    @Override
    public void exeHttpRequest(HashMap<String, String> params, Class modelClass, String TAG, Response.Listener listener) {
        BFJSONRequest request = new BFJSONRequest(modelClass, params, listener);
        if(StringUtils.isNotBlank(TAG))
            request.setTag(TAG);
        HttpRequestLoader.getInstance(this).startHttpLoader(request);
    }

    @Override
    public View getRootView() {
        return null;
    }

    @Override
    public View getBaseRootView() {
        return getRootView();
    }

    @Override
    public void tooglePush() {
        if(PushSetting.isPushOn(this)) {
            PushSetting.unregistPush(this);
        } else {
            PushSetting.registPush(this);
        }
    }

    @Override
    public boolean isPushOn() {
        return PushSetting.isPushOn(this);
    }

    @Override
    public void showShareLayout(OnShareChannelSelected selector) {

    }

    @Override
    public void registToReceiveKeyBackGroup(CanCloseLayout layout) {
        registToGroup(layout);
    }

    @Override
    public void onReady() {

    }

    public boolean dismissShareLayout() {
        if(mShareLayout != null && mShareLayout.getVisibility() == View.VISIBLE) {
            manageShareLayout(false);
            return true;
        }
        return false;
    }

    public View generatorShareLayout() {
        mShareLayout = View.inflate(this, R.layout.share_channel_layout, null);
        mShareWechat = mShareLayout.findViewById(R.id.share_wechat);
        mShareTimeline = mShareLayout.findViewById(R.id.share_timeline);
        mShareWeibo = mShareLayout.findViewById(R.id.share_weibo);
        return mShareLayout;
    }

    public void manageShareLayout(final boolean show) {
        ValueAnimator anim = ValueAnimator.ofFloat(show ? 0 : 1, show ? 1 : 0);
        anim.setDuration(400);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();

                mShareLayout.setAlpha(value);
                mShareWechat.setAlpha(value);
                mShareWeibo.setAlpha(value);
                mShareTimeline.setAlpha(value);

                if(show) {
                    float translationY = mShareWechat.getHeight() * (1-value);
                    mShareWechat.setTranslationY(translationY);
                    mShareWeibo.setTranslationY(translationY);
                    mShareTimeline.setTranslationY(translationY);
                }
            }
        });
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if(!show) {
                    mShareLayout.setVisibility(View.GONE);
                }
            }

            @Override
            public void onAnimationStart(Animator animation) {
                if(show) {
                    mShareLayout.setAlpha(0f);
                    mShareWechat.setAlpha(0f);
                    mShareWeibo.setAlpha(0f);
                    mShareTimeline.setAlpha(0f);
                    mShareLayout.setVisibility(View.VISIBLE);
                }
            }
        });
        anim.start();
    }

}
